package com.stone.persistent.recyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.stone.persistent.recyclerview.adapter.MainListAdapter;
import com.stone.persistent.recyclerview.extensions.ActivityExtensions;
import com.stone.persistent.recyclerview.helper.SyncScrollHelpers;
import com.stone.persistent.recyclerview.library.ParentRecyclerView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


public class MainActivity extends AppCompatActivity {

//    ConstraintLayout main_toolbar;
//    ConstraintLayout main_search_layout;
//    ImageView main_back_img1,main_back_img2,main_top_logo;
//    FrameLayout home_float_layout;

    ParentRecyclerView parentRecyclerView;
    SmartRefreshLayout smartRefreshLayout;
    MainListAdapter listAdapter;

    private Handler uiHandler;
    private static final int MSG_TYPE_REFRESH_FINISHED = 0;
    private static final int MSG_TYPE_TABS_LOADED = 1;


    @Override
    protected void onResume() {
        super.onResume();
        myApplication.getContext().isRefesh = true;
    }

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        ActivityExtensions.immerseStatusBar(this);
        this.initHandler();
        this.initAdapter();


//        main_toolbar = (ConstraintLayout) this.findViewById(R.id.main_toolbar);
//        main_search_layout = (ConstraintLayout) this.findViewById(R.id.main_search_layout);

//        main_back_img1 = (ImageView) this.findViewById(R.id.main_back_img1);
//        main_back_img2 = (ImageView) this.findViewById(R.id.main_back_img2);
//        main_top_logo = (ImageView) this.findViewById(R.id.main_top_logo);
//        home_float_layout = (FrameLayout) this.findViewById(R.id.home_float_layout);

        parentRecyclerView = (ParentRecyclerView) this.findViewById(R.id.main_recycler_view);
        parentRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        parentRecyclerView.setAdapter(listAdapter);

        SyncScrollHelpers syncScrollHelper = new SyncScrollHelpers(this);
        syncScrollHelper.initLayout();
        syncScrollHelper.syncRecyclerViewScroll(parentRecyclerView);

        smartRefreshLayout = (SmartRefreshLayout) this.findViewById(R.id.main_refresh_layout);
        syncScrollHelper.syncRefreshPullDown(smartRefreshLayout);


        ((SmartRefreshLayout) this.findViewById(R.id.main_refresh_layout)).setOnRefreshListener(new OnRefreshListener() {
            public final void onRefresh(@NotNull RefreshLayout it) {
                if (uiHandler != null) {
                    uiHandler.sendEmptyMessageDelayed(MSG_TYPE_REFRESH_FINISHED, 800L);
                }
            }
        });
    }

    private final void initHandler() {
        uiHandler = new Handler((new Handler.Callback() {
            public final boolean handleMessage(Message it) {
                if (it.what == MSG_TYPE_REFRESH_FINISHED) {
                    ((SmartRefreshLayout) MainActivity.this.findViewById(R.id.main_refresh_layout)).finishRefresh();
                    listAdapter.setTabsLoaded(false);
                    myApplication.getContext().isRefesh = true;
                } else if (it.what == MSG_TYPE_TABS_LOADED) {
                    if (listAdapter != null) {
                        listAdapter.onTabsLoaded();
                    }
                }
                return false;
            }
        }));
    }

    private final void initAdapter() {
        listAdapter = new MainListAdapter(this);
        listAdapter.setLoadingTabsListener(new MainListAdapter.LoadingTabsListener() {
            @Override
            public void invoke() {
                if (uiHandler != null) {
                    uiHandler.sendEmptyMessageDelayed(MSG_TYPE_TABS_LOADED, 800L);
                }
            }
        });
    }


}
